package com.sanri.tools.modules.mock.l7.websocket.chat.service;

import com.sanri.tools.modules.core.dtos.param.PageParam;
import com.sanri.tools.modules.core.security.UserService;
import com.sanri.tools.modules.core.utils.OnlyPath;
import com.sanri.tools.modules.mock.l7.websocket.chat.domain.ChatContact;
import com.sanri.tools.modules.mock.l7.websocket.chat.domain.Message;
import com.sanri.tools.modules.mock.l7.websocket.chat.repository.ChatUserRepository;
import com.sanri.tools.modules.mock.l7.websocket.chat.repository.MediaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    private ChatService chatService;
    @Autowired
    private ChatUserRepository chatUserRepository;
    @Autowired(required = false)
    private UserService userService;

    @Autowired
    private MediaRepository mediaRepository;

    /**
     * 联系人列表
     * @return
     */
    @GetMapping("/contacts")
    public List<ChatContact> contacts() throws IOException {
        List<ChatContact> chatThumbnails = chatService.contacts();
        return chatThumbnails;
    }

    /**
     * 添加联系人
     * @param contact
     */
    @PostMapping("/contact/add")
    public void addContact(String contact) throws IOException {
        chatUserRepository.addContact(userService.username(), contact);
    }

    /**
     * 联系消息
     * @param contact
     * @param pageParam
     * @return
     * @throws IOException
     */
    @GetMapping("/messages")
    public List<Message> messages(String contact, PageParam pageParam) throws IOException {
        return chatService.messages(contact,pageParam);
    }

    /**
     * 开启 chat
     * @throws IOException
     */
    @PostMapping("/openChat")
    public void openChat() throws IOException {
        chatService.openChat();
    }

    /**
     * 客户端图片/文件上传
     * @return
     */
    @PostMapping({"/upload/file","/upload/image","/upload/sound","/upload/video"})
    public String uploadMedia(@RequestParam("file") MultipartFile file) throws IOException {
        return mediaRepository.storageMedia(file);
    }

    /**
     * http 方式发送消息
     * @param message
     */
    @PostMapping("/sendMessage")
    public void sendMessage(@RequestBody Message message) throws IOException {
        chatService.sendMessage(message);
    }
}
